#include "BinaryNode.h"
#include "Global.h"

BinaryNode::BinaryNode(void){
	LNode = RNode = NULL;
}

BinaryNode::BinaryNode(int left, int top, int right, int bottom, string iid){
	rect.left = left;
	rect.top = top;
	rect.right = right;
	rect.bottom = bottom;
	
	id = iid;
	LNode = RNode = NULL;
}

BinaryNode::~BinaryNode(void){}

void BinaryNode::GetListObj(string ilist, int left, int right){
	
	if (ilist.compare("0") == 0)
	{
		//trace(L"return");
		return;
	}

	istringstream iss(ilist);
	
	while(iss.good())
	{
		string id;
		iss >> id;
		int id_temp = atoi(id.c_str());
		//trace(L"%d", id_temp);
		if (id_temp == 0)
		{
			//trace(L"return cause id = 0");
			return;
		}
		list<Static*>::iterator ii;
		for (ii = Global::listStaticObj.begin(); ii != Global::listStaticObj.end(); ++ii)
		{
			if (id_temp == (*ii)->id)
			{
				listObject.push_back((*ii));				
			}
		}
	}
	iss.str(string());
	iss.clear();
}
void BinaryNode::Release(){
	list<Static*>::iterator ii;
	Static *temp;
	while(!listObject.empty()){
		list<Static*>::iterator it = listObject.begin();
		temp = (*it);
		listObject.pop_front();
		delete temp;
	}
}
void BinaryNode::Render(){
	
	list<Static*>::iterator it;
	for(it = listObject.begin(); it != listObject.end(); ++it )
	{
		(*it)->Render();		
	}
}

void BinaryNode::Update(){	
	list<Static*>::iterator it;
	for(it = listObject.begin(); it != listObject.end(); ++it ){
		(*it)->Update();		
		if (CheckExitsStaticObj((*it)))
			Global::listStaticObjRender.push_back((*it));					
	}

}
bool BinaryNode::CheckExitsStaticObj(Static *obj)
{
	list<Static*>::iterator it;
	for(it = Global::listStaticObjRender.begin(); it != Global::listStaticObjRender.end(); ++it )
	{
		if ((*it)->id == obj->id)
			return false;		
	}
	return true;
}